package vista.RegistroParking;

import Utilidades.Fechas;
import controlador.RegistroParkingControlador;
import vista.*;
import java.util.Date;
import javax.swing.JPanel;
import modelo.RegistroParkingModelo;
import modelo.VehiculoModelo;
import modelo.entidades.Parking;
import vista.Parking.ParkingVista;
import Comprobaciones.GestionDinamicaParkings;

/**
 *
 * @author jjcamacho
 */
public class RegistroParkingVistaImp extends AbstractVistaImp<RegistroParkingControlador, RegistroParkingModelo> implements RegistroParkingVista {

    private Parking parking;
    private ParkingVista panelInformativo;
    private ParkingVista panelBarrera;
    private ParkingVista panelAviso;
    private VehiculoModelo vehiculoModelo;
    private GestionDinamicaParkings gestionDinamicaParkings = new GestionDinamicaParkings();

    public RegistroParkingVistaImp(Parking parking, ParkingVista panelInformativo, ParkingVista panelBarrera, ParkingVista panelAviso, VehiculoModelo vehiculoModelo) {
        this.parking = parking;
        this.panelInformativo = panelInformativo;
        this.panelBarrera = panelBarrera;
        this.panelAviso = panelAviso;
        this.vehiculoModelo = vehiculoModelo;
        initComponents();
        pnlInformativo.add((JPanel) panelInformativo);
        pnlBarrera.add((JPanel) panelBarrera);
        pnlAviso.add((JPanel) panelAviso);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bnEntrar = new javax.swing.JButton();
        bnSalir = new javax.swing.JButton();
        imgCamara = new javax.swing.JLabel();
        pnlDatos = new javax.swing.JPanel();
        lbMatricula = new javax.swing.JLabel();
        txtMatricula = new javax.swing.JTextField();
        imgBarrera = new javax.swing.JLabel();
        pnlInformativo = new javax.swing.JPanel();
        pnlBarrera = new javax.swing.JPanel();
        pnlAviso = new javax.swing.JPanel();

        setPreferredSize(new java.awt.Dimension(395, 245));

        bnEntrar.setMnemonic('E');
        bnEntrar.setText("Entrar");
        bnEntrar.setDisplayedMnemonicIndex(0);
        bnEntrar.setEnabled(false);
        bnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnEntrarActionPerformed(evt);
            }
        });

        bnSalir.setMnemonic('S');
        bnSalir.setText("Salir");
        bnSalir.setDisplayedMnemonicIndex(0);
        bnSalir.setEnabled(false);
        bnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnSalirActionPerformed(evt);
            }
        });

        imgCamara.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/Img/Camara.PNG"))); // NOI18N

        lbMatricula.setText("Matrícula");

        txtMatricula.setFont(new java.awt.Font("Arial", 1, 36));
        txtMatricula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMatriculaKeyReleased(evt);
            }
        });

        imgBarrera.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/Img/Barrera.png"))); // NOI18N
        imgBarrera.setAutoscrolls(true);

        pnlInformativo.setLayout(new javax.swing.BoxLayout(pnlInformativo, javax.swing.BoxLayout.LINE_AXIS));

        pnlBarrera.setLayout(new javax.swing.BoxLayout(pnlBarrera, javax.swing.BoxLayout.LINE_AXIS));

        pnlAviso.setLayout(new javax.swing.BoxLayout(pnlAviso, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout pnlDatosLayout = new javax.swing.GroupLayout(pnlDatos);
        pnlDatos.setLayout(pnlDatosLayout);
        pnlDatosLayout.setHorizontalGroup(
            pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbMatricula)
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addComponent(imgBarrera)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlBarrera, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                    .addComponent(pnlAviso, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addComponent(pnlInformativo, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addComponent(txtMatricula, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlDatosLayout.setVerticalGroup(
            pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosLayout.createSequentialGroup()
                .addComponent(lbMatricula)
                .addGap(1, 1, 1)
                .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(imgBarrera, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlDatosLayout.createSequentialGroup()
                        .addComponent(pnlAviso, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pnlBarrera, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlInformativo, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(imgCamara, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addComponent(bnEntrar, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlDatos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(imgCamara)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                        .addComponent(bnEntrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bnSalir)
                        .addGap(18, 18, 18))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnEntrarActionPerformed
        txtMatricula.setText(txtMatricula.getText().toUpperCase());
        getController().fireEntradaVehiculo(getParking(), panelInformativo, txtMatricula.getText(), Fechas.fechaATexto(new Date()));
        gestionDinamicaParkings.comprobarParking(parking.getNombre());
    }//GEN-LAST:event_bnEntrarActionPerformed

    private void bnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnSalirActionPerformed
        txtMatricula.setText(txtMatricula.getText().toUpperCase());
        getController().fireSalidaVehiculo(getParking(), panelInformativo, txtMatricula.getText(), Fechas.fechaATexto(new Date()));
    }//GEN-LAST:event_bnSalirActionPerformed

    private void txtMatriculaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatriculaKeyReleased
        bnEntrar.setEnabled(txtMatricula.getText().length() >= 5); //intentamos que la matricula sea algo mas que un simple caracter
        bnSalir.setEnabled(bnEntrar.isEnabled());
    }//GEN-LAST:event_txtMatriculaKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnEntrar;
    private javax.swing.JButton bnSalir;
    private javax.swing.JLabel imgBarrera;
    private javax.swing.JLabel imgCamara;
    private javax.swing.JLabel lbMatricula;
    private javax.swing.JPanel pnlAviso;
    private javax.swing.JPanel pnlBarrera;
    private javax.swing.JPanel pnlDatos;
    private javax.swing.JPanel pnlInformativo;
    private javax.swing.JTextField txtMatricula;
    // End of variables declaration//GEN-END:variables

    @Override
    public void refresh() {
        //No requiere implementación ya que son las vistas incluidas las que se refrescan
    }

    public void setParking(Parking parking) {
        this.parking = parking;
    }

    public Parking getParking() {
        return parking;
    }
}


